package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.dto.GoodsSalesDTO;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.entity.Orders;
import com.sky.vo.OrderVO;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 目标:
 *
 * @author YUE
 * @date 2024/6/18 11:44
 */
@Mapper
public interface OrderMapper {
    /*
      添加订单对象
     */
    @Insert("insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, cancel_time, estimated_delivery_time, delivery_status, delivery_time, pack_amount, tableware_number, tableware_status) " +
            "values (#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus},#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason},#{cancelTime},#{estimatedDeliveryTime},#{deliveryStatus},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    public void inster(Orders orders);

    @Select("select * from orders where number = #{number}")
    Orders getByNumberAndUserId(String outTradeNo, Long userId);

    void update(Orders orders);

    List<OrderVO> getOrderPage(OrdersPageQueryDTO ordersPageQueryDTO);

    // 根据id查询订单
    @Select("select * from orders where id = #{id}")
    Orders getOrderById(Long id);


    @Update("update orders set status = 6 where id = #{id}")
    void cancle(Long id);

     /*
       分页条件查询
     */
    Page<Orders> adminPage(OrdersPageQueryDTO ordersPageQueryDTO);


     /*
       各个状态的订单数量统计
     */
    @Select("select count(id) from orders where status = #{status}")
    Integer counts(Integer toBeConfirmed);

    // 查询指定时间 指定状态的订单
    @Select("select * from orders where status = #{status} and order_time < #{orderTime}")
    List<Orders> getByStatusAndTimeLT(Integer status, LocalDateTime orderTime);

    // 查询有效订单 status = 5
    @Select("select count(*) from orders where status = 5 and order_time between #{beginTime} and #{endTime}")
    Integer validOrders(LocalDateTime beginTime, LocalDateTime endTime);

    // 查询营业额
    @Select("select sum(amount) from orders where status = 5 and order_time between #{beginTime} and #{endTime} ")
    Double turnover(LocalDateTime beginTime, LocalDateTime endTime);

    // 所有订单数
    @Select("select count(*) from orders  where order_time between #{beginTime} and #{endTime}")
    Integer getAllOrders(LocalDateTime beginTime, LocalDateTime endTime);

    /*
       查询营业额
     */
    Double getByMap(Map map);

    /*
      用户统计
     */
    Integer getUserByMap(Map map);

    /*
       订单数
     */
    Integer countByMap(Map map);

    /*
      查询销量排名top10接口
     */
    List<GoodsSalesDTO> getSalesTop10();

    @Select("select count(id) from setmeal where status = 1 ")
    Integer getStart(LocalDateTime beginTime, LocalDateTime endTime);
    @Select("select count(id) from setmeal where status = 0 ")
    Integer getStop(LocalDateTime beginTime, LocalDateTime endTime);

    // 查询起售中的菜品
    @Select("select count(id) from dish where status = 1  ")
    Integer getOrdersStart();
    // 查询停售中的菜品
    @Select("select count(id) from dish where status = 0  ")
    Integer getOrdersStop();

    // 根据状态查询订单数量
    Integer getByStatus(HashMap map);
}